package cyapps.gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import javax.swing.KeyStroke;


public class MainFrame extends JFrame {
	
	private static final long serialVersionUID = 1L;
	
	private final JMenuBar    menuBar          = new JMenuBar();
	private final JMenu       mFile            = new JMenu(Messages.getString("menu.file"));
	private final JMenu       mHelp            = new JMenu(Messages.getString("menu.help"));
	
	private final MainPanel   panel            = new MainPanel();
	
	
	public MainFrame() {
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setTitle(Messages.getString("menu.title"));
		createMenu();
		add(panel);
		setSize(420, 400);
		setMinimumSize(new Dimension(420, 200));
		panel.newFile();
	}
	
	
	private void createMenu() {
		setJMenuBar(menuBar);
		
		menuBar.add(mFile);
		mFile.add(new JMenuItem(new AbstractAction() {// New
			private static final long serialVersionUID = 1L;
			{
				putValue(NAME, Messages.getString("menu.new")); //$NON-NLS-1$
				putValue(SHORT_DESCRIPTION, Messages.getString("menu.newShort")); //$NON-NLS-1$
				putValue(LONG_DESCRIPTION, Messages.getString("menu.newLong")); //$NON-NLS-1$
				putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
			}
			@Override
			public void actionPerformed(ActionEvent e) {
				panel.newFile();
			}
		}));
		mFile.add(new JMenuItem(new AbstractAction() {// Open
			private static final long serialVersionUID = 1L;
			{
				putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
				putValue(NAME, Messages.getString("menu.open")); //$NON-NLS-1$
				putValue(SHORT_DESCRIPTION, Messages.getString("menu.openShort")); //$NON-NLS-1$
				putValue(LONG_DESCRIPTION, Messages.getString("menu.openLong")); //$NON-NLS-1$
			}
			@Override
			public void actionPerformed(ActionEvent e) {
				panel.openFile();
			}
		}));
		mFile.add(new JSeparator());
		mFile.add(new JMenuItem(new AbstractAction() {// Save
			private static final long serialVersionUID = 1L;
			{
				putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
				putValue(NAME, Messages.getString("menu.save")); //$NON-NLS-1$
				putValue(SHORT_DESCRIPTION, Messages.getString("menu.saveShort")); //$NON-NLS-1$
				putValue(LONG_DESCRIPTION, Messages.getString("menu.saveLong")); //$NON-NLS-1$
			}
			@Override
			public void actionPerformed(ActionEvent e) {
				panel.saveFile();
			}
		}));
		mFile.add(new JMenuItem(new AbstractAction() {// Save as
			private static final long serialVersionUID = 1L;
			{
				putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK
				        | InputEvent.SHIFT_MASK));
				putValue(NAME, Messages.getString("menu.saveAs")); //$NON-NLS-1$
				putValue(SHORT_DESCRIPTION, Messages.getString("menu.saveAsShort")); //$NON-NLS-1$
				putValue(LONG_DESCRIPTION, Messages.getString("menu.saveAsLong")); //$NON-NLS-1$
			}
			@Override
			public void actionPerformed(ActionEvent e) {
				panel.saveFileAs();
			}
		}));
		mFile.add(new JSeparator());
		mFile.add(new JMenuItem(new AbstractAction() {// Exit
			private static final long serialVersionUID = 1L;
			{
				putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.ALT_MASK));
				putValue(NAME, Messages.getString("menu.exit")); //$NON-NLS-1$
				putValue(SHORT_DESCRIPTION, Messages.getString("menu.exitShort")); //$NON-NLS-1$
				putValue(LONG_DESCRIPTION, Messages.getString("menu.exitLong")); //$NON-NLS-1$
			}
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		}));
		
		menuBar.add(mHelp);
		mHelp.add(new JMenuItem(new AbstractAction() {// About
			private static final long serialVersionUID = 1L;
			{
				putValue(NAME, Messages.getString("menu.about")); //$NON-NLS-1$
				putValue(SHORT_DESCRIPTION, Messages.getString("menu.aboutShort")); //$NON-NLS-1$
				putValue(LONG_DESCRIPTION, Messages.getString("menu.aboutLong")); //$NON-NLS-1$
			}
			@Override
			public void actionPerformed(ActionEvent e) {
			}
		}));
	}
}